package com.example.project.utils.file;

/**
 * 媒体类型工具类
 * 
 * @author ruoyi
 */
public class MimeTypeUtils {

	public static final String IMAGE_PNG = "image/png";

	public static final String IMAGE_JPG = "image/jpg";

	public static final String IMAGE_JPEG = "image/jpeg";

	public static final String IMAGE_BMP = "image/bmp";

	public static final String IMAGE_GIF = "image/gif";

	public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" };

	public static final String[] FLASH_EXTENSION = { "swf", "flv" };

	public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
			"asf", "rm", "rmvb" };

	public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" };

	public static final String[] DEFAULT_ALLOWED_EXTENSION = {
			// 图片
			"bmp", "gif", "jpg", "jpeg", "png",
			// word excel powerpoint
			"doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
			// 压缩文件
			"rar", "zip", "gz", "bz2",
			// 视频格式
			"mp4", "avi", "rmvb",
			// pdf
			"pdf" };

	public static String getExtension(String prefix) {
		switch (prefix) {
		case IMAGE_PNG:
			return "png";
		case IMAGE_JPG:
			return "jpg";
		case IMAGE_JPEG:
			return "jpeg";
		case IMAGE_BMP:
			return "bmp";
		case IMAGE_GIF:
			return "gif";
		default:
			return "";
		}
	}

	/**
	 * 根据文件名检测MIME类型
	 * 
	 * @param filename 文件名
	 * @return MIME类型，如果无法识别则返回null
	 */
	public static String detectMimeType(String filename) {
		if (filename == null || filename.isEmpty()) {
			return null;
		}

		String lowerFilename = filename.toLowerCase();
		
		// 图片类型
		if (lowerFilename.endsWith(".png")) {
			return IMAGE_PNG;
		} else if (lowerFilename.endsWith(".jpg") || lowerFilename.endsWith(".jpeg")) {
			return IMAGE_JPEG;
		} else if (lowerFilename.endsWith(".gif")) {
			return IMAGE_GIF;
		} else if (lowerFilename.endsWith(".bmp")) {
			return IMAGE_BMP;
		}
		
		// 文档类型
		else if (lowerFilename.endsWith(".pdf")) {
			return "application/pdf";
		} else if (lowerFilename.endsWith(".doc")) {
			return "application/msword";
		} else if (lowerFilename.endsWith(".docx")) {
			return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
		} else if (lowerFilename.endsWith(".xls")) {
			return "application/vnd.ms-excel";
		} else if (lowerFilename.endsWith(".xlsx")) {
			return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
		} else if (lowerFilename.endsWith(".ppt")) {
			return "application/vnd.ms-powerpoint";
		} else if (lowerFilename.endsWith(".pptx")) {
			return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
		} else if (lowerFilename.endsWith(".txt")) {
			return "text/plain";
		} else if (lowerFilename.endsWith(".html") || lowerFilename.endsWith(".htm")) {
			return "text/html";
		} else if (lowerFilename.endsWith(".xml")) {
			return "application/xml";
		} else if (lowerFilename.endsWith(".json")) {
			return "application/json";
		}
		
		// 压缩文件类型
		else if (lowerFilename.endsWith(".zip")) {
			return "application/zip";
		} else if (lowerFilename.endsWith(".rar")) {
			return "application/x-rar-compressed";
		} else if (lowerFilename.endsWith(".7z")) {
			return "application/x-7z-compressed";
		} else if (lowerFilename.endsWith(".tar")) {
			return "application/x-tar";
		} else if (lowerFilename.endsWith(".gz")) {
			return "application/gzip";
		}
		
		// 音频视频类型
		else if (lowerFilename.endsWith(".mp3")) {
			return "audio/mpeg";
		} else if (lowerFilename.endsWith(".wav")) {
			return "audio/wav";
		} else if (lowerFilename.endsWith(".mp4")) {
			return "video/mp4";
		} else if (lowerFilename.endsWith(".avi")) {
			return "video/x-msvideo";
		} else if (lowerFilename.endsWith(".mov")) {
			return "video/quicktime";
		} else if (lowerFilename.endsWith(".flv")) {
			return "video/x-flv";
		}
		
		// 代码文件类型
		else if (lowerFilename.endsWith(".java")) {
			return "text/x-java-source";
		} else if (lowerFilename.endsWith(".js")) {
			return "application/javascript";
		} else if (lowerFilename.endsWith(".css")) {
			return "text/css";
		} else if (lowerFilename.endsWith(".php")) {
			return "application/x-httpd-php";
		} else if (lowerFilename.endsWith(".py")) {
			return "text/x-python";
		} else if (lowerFilename.endsWith(".cpp") || lowerFilename.endsWith(".c")) {
			return "text/x-c";
		} else if (lowerFilename.endsWith(".cs")) {
			return "text/x-csharp";
		}
		
		// 其他常见类型
		else if (lowerFilename.endsWith(".csv")) {
			return "text/csv";
		} else if (lowerFilename.endsWith(".rtf")) {
			return "application/rtf";
		} else if (lowerFilename.endsWith(".svg")) {
			return "image/svg+xml";
		} else if (lowerFilename.endsWith(".ico")) {
			return "image/x-icon";
		}
		
		// 如果无法识别文件类型，返回null
		return null;
	}
}
